package com.steel.yfmall.cloud.assistant.view.impl;

import com.alibaba.fastjson.JSONObject;
import com.steel.yfmall.cloud.assistant.bean.param.zg.APIParaWarrantyBean;
import com.steel.yfmall.cloud.assistant.bean.result.APIResultWarrantyBean;
import com.steel.yfmall.cloud.assistant.dto.para.goods.ParaProductLibProductFromBean;
import com.steel.yfmall.cloud.assistant.dto.para.user.ParaUserCompanyBean;
import com.steel.yfmall.cloud.assistant.dto.result.goods.ResultProductLibProductFromBean;
import com.steel.yfmall.cloud.assistant.dto.zg.ZgSOResultBean;
import com.steel.yfmall.cloud.assistant.dto.zg.ZgViewBean;
import com.steel.yfmall.cloud.assistant.goods.ProductLibProductFromService;
import com.steel.yfmall.cloud.assistant.po.user.UserCompany;
import com.steel.yfmall.cloud.assistant.user.UserCompanyService;
import com.steel.yfmall.cloud.assistant.utils.constants.ZgConstants;
import com.steel.yfmall.cloud.assistant.utils.zg.ZgViewUtil;
import com.steel.yfmall.cloud.assistant.view.WarrantyService;
import com.steel.yfmall.cloud.assistant.zg.ZgSOService;
import com.steel.yfmall.cloud.components.utils.base.APIMsgBean;
import com.steel.yfmall.cloud.components.utils.base.Pages;
import com.steel.yfmall.cloud.components.utils.base.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author yuchaohua
 * @version V1.0
 * @Title: WarrantyServiceImpl
 * @Package com.steel.yfmall.cloud.assistant.view.impl
 * @Description: (用一句话描述该文件做什么)
 * @date 2021/2/19 11:37
 */
@Slf4j
@Service
public class WarrantyServiceImpl implements WarrantyService {

    /**
     * 获取质保书信息
     * @Title: getWarrantyFromZg
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param apiParaBean 参数说明
     * @return com.steel.yfmall.cloud.components.utils.base.APIMsgBean<com.steel.yfmall.cloud.components.utils.base.Pages<com.steel.yfmall.cloud.assistant.bean.result.APIResultWarrantyBean>>    返回类型
     * @author yuchaohua
     * @date 2021/2/19 13:46
     */
    @Override
    public APIMsgBean<Pages<APIResultWarrantyBean>> getWarrantyFromZg(APIParaWarrantyBean apiParaBean) {
        APIMsgBean<Pages<APIResultWarrantyBean>> apiMsgBean = new APIMsgBean<>();
        apiMsgBean.setSoaStateBean(ResponseCode.buildEnumSuccessSOAStateBean());
        ZgViewBean zgViewBean = new ZgViewBean();
        zgViewBean.setMethod(ZgViewUtil.GET_VIEW_NAME1);
        zgViewBean.setViewId(ZgViewUtil.WARRANTY_TC_01_VIEW);
        Map<String,Object> templateBean = new HashMap<>();
        // 拼接查询条件
        getWarrantyParam(apiParaBean,templateBean);
        zgViewBean.setPageNum(apiParaBean.getCurrentPage() + "");
        zgViewBean.setPageSize(apiParaBean.getPageSize() + "");
        zgViewBean.setParam(templateBean);
        Pages<APIResultWarrantyBean> resultPageBean = new Pages<>();
        //查询tp01商品无主库存信息请求中冠
        ZgSOResultBean viewData = zgSOService.getViewData(zgViewBean);
        //请求中冠成功后
        if (viewData != null && "Y".equals(viewData.getIsSuccess())) {
            // 分页信息
            resultPageBean.paging(apiParaBean.getCurrentPage(),apiParaBean.getPageSize(),viewData.getZgpageData().getRowNums());
            List<APIResultWarrantyBean> warrantyBeanList = null;
            try {
                warrantyBeanList = JSONObject.parseArray(viewData.getData(), APIResultWarrantyBean.class);
                if (!CollectionUtils.isEmpty(warrantyBeanList)) {
                    fillWarrantyBeanList(warrantyBeanList);
                    resultPageBean.setList(warrantyBeanList);
                }
            } catch (Exception e) {
                log.error("质保书信息解析出错，错误信息：" , e);
                apiMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("查询质保书信息失败，请稍后重试"));
            }
        }
        apiMsgBean.setData(resultPageBean);
        return apiMsgBean;
    }

    public void fillWarrantyBeanList(List<APIResultWarrantyBean> warrantyBeanList){
        if (CollectionUtils.isEmpty(warrantyBeanList)){
            return;
        }
        Set<String> customNoList = new HashSet<>();
        Set<String> prodTypeNoList = new HashSet<>();
        // 封装客户信息和品名信息
        for(APIResultWarrantyBean itemBean : warrantyBeanList){
            if (StringUtils.isNotBlank(itemBean.getCustomerNo())){
                customNoList.add(itemBean.getCustomerNo());
            }
            if (StringUtils.isNotBlank(itemBean.getProdTypeNo())){
                prodTypeNoList.add(itemBean.getProdTypeNo());
            }
        }
        Map<String, UserCompany> userCompanyMap = null;
        if (CollectionUtils.isNotEmpty(customNoList)){
            ParaUserCompanyBean paraCompanyBean = new ParaUserCompanyBean();
            paraCompanyBean.setCompanyCodeList(new ArrayList<>(customNoList));
            userCompanyMap = userCompanyService.getUserCompanyCodeMap(paraCompanyBean);
        }
        Map<String, ResultProductLibProductFromBean> libProductFromBeanMap = null;
        if (CollectionUtils.isNotEmpty(customNoList)){
            ParaProductLibProductFromBean paraFromBean = new ParaProductLibProductFromBean();
            paraFromBean.setProductFormCodeList(new ArrayList<>(prodTypeNoList));
            libProductFromBeanMap = productLibProductFromService.getProductLibFromBeanMap(paraFromBean);
        }

        // 封装客户信息和品名信息
        for(APIResultWarrantyBean itemBean : warrantyBeanList){
            if (StringUtils.isNotBlank(itemBean.getCustomerNo()) && userCompanyMap != null && userCompanyMap.containsKey(itemBean.getCustomerNo())){
                itemBean.setCustomerName(userCompanyMap.get(itemBean.getCustomerNo()).getCompanyName());
            }
            if (StringUtils.isNotBlank(itemBean.getProdTypeNo()) && libProductFromBeanMap != null && libProductFromBeanMap.containsKey(itemBean.getProdTypeNo())){
                itemBean.setCategoryName(libProductFromBeanMap.get(itemBean.getProdTypeNo()).getCategoryName());
            }
            // 质保书下载地址
            if (StringUtils.isNotBlank(itemBean.getMillSheetNo())){
                itemBean.setWarrantyUrl("http://www.jingyebuy.com/zbsfile/"+ itemBean.getMillSheetNo() +".pdf");
            }
        }

    }

    /**
     * 拼接查询参数
     * @param apiParaBean
     * @param templateBean
     */
    private void getWarrantyParam(APIParaWarrantyBean apiParaBean, Map<String, Object> templateBean) {
        if (apiParaBean == null || templateBean == null) {
            return;
        }

        // 客商编号
        if (StringUtils.isNotBlank(apiParaBean.getCustomerNo())){
            templateBean.put("customerNo", paraConditionMap(ZgConstants.eq, apiParaBean.getCustomerNo()));
        }
        // 客商编号
        if (StringUtils.isNotBlank(apiParaBean.getCustomerNoList())){
            templateBean.put("customerNo", paraConditionMap(ZgConstants.in, apiParaBean.getCustomerNoList()));
        }
        //  质保书编号
        if (StringUtils.isNotBlank(apiParaBean.getMillSheetNo())){
            templateBean.put("millSheetNo", paraConditionMap(ZgConstants.eq, apiParaBean.getMillSheetNo()));
        }
        //  订单号
        if (StringUtils.isNotBlank(apiParaBean.getOrderNo())){
            templateBean.put("orderNo", paraConditionMap(ZgConstants.eq, apiParaBean.getOrderNo()));
        }
        //  装车作业单号
        if (StringUtils.isNotBlank(apiParaBean.getTallyNo())){
            templateBean.put("tallyNo", paraConditionMap(ZgConstants.eq, apiParaBean.getTallyNo()));
        }
        //  产品型态
        if (StringUtils.isNotBlank(apiParaBean.getProdTypeNo())){
            templateBean.put("prodTypeNo", paraConditionMap(ZgConstants.eq, apiParaBean.getProdTypeNo()));
        }
        //  产品型态
        if (StringUtils.isNotBlank(apiParaBean.getProdTypeNoList())){
            templateBean.put("prodTypeNo", paraConditionMap(ZgConstants.in, apiParaBean.getProdTypeNoList()));
        }
        //  轧制批号
        if (StringUtils.isNotBlank(apiParaBean.getLotNo())){
            templateBean.put("lotNo", paraConditionMap(ZgConstants.eq, apiParaBean.getLotNo()));
        }
        //  车牌号
        if (StringUtils.isNotBlank(apiParaBean.getCarNo())){
            templateBean.put("carNo", paraConditionMap(ZgConstants.eq, apiParaBean.getCarNo()));
        }
        //  发货日期
        if (StringUtils.isNotBlank(apiParaBean.getShipDate())){
            templateBean.put("shipDate", paraConditionMap(ZgConstants.eq, apiParaBean.getShipDate()));
        }
        //  发货日期大于等于
        if (StringUtils.isNotBlank(apiParaBean.getShipDateStar())){
            templateBean.put("shipDate", paraConditionMap(ZgConstants.greatEq, apiParaBean.getShipDateStar()));
        }
        //  发货日期下于等于
        if (StringUtils.isNotBlank(apiParaBean.getShipDateEnd())){
            templateBean.put("shipDate", paraConditionMap(ZgConstants.lessEq, apiParaBean.getShipDateEnd()));
        }

    }

    //  拼接条件
    public Map<String,String> paraConditionMap(String condition, String value){
        Map<String,String> conditionMap = new HashMap<>();
        conditionMap.put("condition", condition);
        conditionMap.put("value", value);
        return conditionMap;
    }


    @Resource
    private ZgSOService zgSOService;

    @Resource
    private UserCompanyService userCompanyService;

    @Resource
    private ProductLibProductFromService productLibProductFromService;

}
